package com.qd.p2p.web.action.notify.redenvelope;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Qualifier;

import com.qd.p2p.web.action.notify.TradeNotifyBaseAction;
import com.qd.p2p.web.action.notify.TradeNotifyValueEvent;
import com.qdlc.p2p.biz.processor.event.RedEnvelopeValueEvent;
import com.qdlc.p2p.biz.service.CollectPayService;
import com.qdlc.p2p.common.constant.NotifyUrl;
import com.qdlc.p2p.common.core.queue.QueueHandlers;
import com.qdlc.p2p.common.util.MailUtil;
import com.qdlc.p2p.dal.common.SystemConfigHelper;
import com.qdlc.p2p.dal.dto.TppTradeLog;

/**
 * 红包兑换 - 代收 回调通知
 *
 * @author wuaj 2016-01-21
 */
public class NotifyCollectAction extends TradeNotifyBaseAction {

    /** 业务名 */
    private final static String BUSINESS_NAME = "红包兑换";

    /** 业务处理名 */
    private final static String PROCESS_NAME = "代收";

    /** 代收业务处理 */
    @Resource
    @Qualifier("redEnvelopeCollectService")
    private CollectPayService redEnvelopeCollectService;

    /** 代付业务处理 */
    @Resource
    @Qualifier("redEnvelopePayService")
    private CollectPayService redEnvelopePayService;

    private static String WEB_URL = SystemConfigHelper.getValue(SystemConfigHelper.Nid.WEB_URL);

    /**
     * 红包兑现 - 代收回调处理
     */
    @Override
    @Action(value = NotifyUrl.RED_ENVELOPE_NOTIFY_COLLECT_URL)
    public void doAction() {
        actionHandle();
    }

    @Override
    public String getBusinessName() {
        return BUSINESS_NAME;
    }

    @Override
    public String getProcessName() {
        return PROCESS_NAME;
    }

    /**
     * 完成状态 业务处理
     */
    @Override
    public void finishHandle(TradeNotifyValueEvent event) {
        // 代收完成业务处理
        redEnvelopeCollectService.tradeFinish(event.getTppTradeLog(), event.getDepositStatus(), event.getDepositAmount());

        try {
            TppTradeLog tppTradeLog = redEnvelopePayService.createOrder(event.getTppTradeLog());

            // 红包兑现代付放入队列中 以免影响代收回调的时间
            RedEnvelopeValueEvent redEnvelopeValueEvent = new RedEnvelopeValueEvent();
            redEnvelopeValueEvent.setTppTradeLog(tppTradeLog);
            //将事件放入到队列中
            QueueHandlers.put(redEnvelopeValueEvent);
        } catch (Exception ex) {
            ex.printStackTrace();
            // 通知重新发起 红包代付
            String reCallUrl = String.format(NotifyUrl.RE_RED_ENVELOPE_PAY_URL, WEB_URL, event.getTppTradeLog().getOrderNo());
            MailUtil.sendMail("兑现代付-订单生成失败", "代付订单生成失败", reCallUrl);
        }
    }

    /**
     * 成功状态 业务处理
     */
    @Override
    public void successHandle(TradeNotifyValueEvent event) {
        redEnvelopeCollectService.tradeSuccess(event.getTppTradeLog(), event.getDepositStatus());
    }

    /**
     * 失败状态 业务处理
     */
    @Override
    public void failHandle(TradeNotifyValueEvent event) {
        redEnvelopeCollectService.tradeFail(event.getTppTradeLog(), event.getDepositStatus());
    }
}
